package com.elite.groovy.callback

import com.elite.groovy.kh.KHUtil
import com.elite.groovy.util.EliteBean
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * @Title: 查询防伪维护时间查询API
 * @author: 陶豪
 *
 */
public    class apiIVRSecurityTime implements ServiceBackInvoke{

    private static final Log log = LogFactory.getLog(apiIVRSecurityTime.class);
    @Override
    String doCallBack(EliteBean eliteBean, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {

        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());

        // 初始化返回参数
        JSONObject result = new JSONObject();

        // 获取接口调用方式及参数
        String getMethod = httpServletRequest.getMethod();
        log.debug("[getMethod]:" + getMethod);

        String inData = "";
        try {
        if (getMethod.equals("GET")) {
            inData = httpServletRequest.getParameter("data");
            log.debug("[原始Get请求参数]:" + inData);
        } else {
            InputStreamReader reader = new InputStreamReader(httpServletRequest.getInputStream(), "UTF-8");
            BufferedReader iny = new BufferedReader(reader);
            String line;
            while ((line = iny.readLine()) != null) {
                inData += line;
            }
            log.debug("[原始Post请求参数]:" + inData);
        }
        log.info("[调用请求信息]:" + inData);

        //  入参为空
        if (KHUtil.isNull(inData)) {
            log.debug("【防伪维护时间查询API】入参为空-无法完成操作!");
            result.put("status", -2);
            result.put("msg", "入参为空-无法完成操作!");
            result.put("data", "{}");
            //写入接口日志表
            String logsql="insert into dg_api_log(rec_guid,api_type,request_body,back_body,request_time,remarks)values(uuid(),?,?,?,now(),?)";
            jt.update(logsql, "apiSecurityTime",inData.toString(),result.toString(),"防伪维护时间查询API接口");
            return result.toString();
        }



            //解析请求的jSON
            JSONObject inJson = JSONObject.fromObject(inData);
            String pretplcode = inJson.getString("pretplcode");

            String segment = inJson.getString("segment");
            String posttplcode = inJson.getString("posttplcode");
            String tag = inJson.getString("tag");

            if (KHUtil.isNull(pretplcode) || KHUtil.isNull(segment) || KHUtil.isNull(posttplcode)||KHUtil.isNull(tag)) {
                log.debug("【防伪维护时间查询API】参数值为空-无法完成操作!");
                result.put("status", -2);
                result.put("msg", "参数值为空-无法完成操作!");
                result.put("data", "{}");
                //写入接口日志表
                String logsql="insert into dg_api_log(rec_guid,api_type,request_body,back_body,request_time,remarks)values(uuid(),?,?,?,now(),?)";
                jt.update(logsql, "apiSecurityTime",inData.toString(),result.toString(),"防伪维护时间查询API接口");
                return result.toString();
            }

            //判断是否是约定好的固定值
            if (!pretplcode.equals("Local_Interface_input")||!segment.equals("formcond")||!posttplcode.equals("Interface_out_detail")||!tag.equals("tianrun")) {
                log.debug("【防伪维护时间查询API】固定值不正确-无法完成操作!");
                result.put("status", -2);
                result.put("msg", "固定值不正确-无法完成操作!");
                result.put("data", "{}");
                //写入接口日志表
                String logsql="insert into dg_api_log(rec_guid,api_type,request_body,back_body,request_time,remarks)values(uuid(),?,?,?,now(),?)";
                jt.update(logsql, "apiSecurityTime",inData.toString(),result.toString(),"防伪维护时间查询API接口");
                return result.toString();
            }

            //校验通过-->开始查找对应参数下的关闭时间
            String selectsql=''' select DATE_FORMAT(starttime,'%Y-%m-%d %H:%i:%s') as starttime  ,DATE_FORMAT(endtime,'%Y-%m-%d %H:%i:%s') as  endtime from ivr_closetime where type_id=?''';
            List<Map<String, Object>>  selelctList=jt.queryForList(selectsql,"FWCODE");
            log.debug("selelctList：" + selelctList);

            String starttime =selelctList.get(0).get("starttime");
            String endtime =selelctList.get(0).get("endtime");


            result.put("InterfaceCode","DGI030");
            result.put("company","davco")
            result.put("posttplcode",posttplcode);
            result.put("pretplcode",pretplcode);
            result.put("tag",tag);
            result.put("uid","/extapi");
            result.put("pretplcode",pretplcode);
            result.put("unserve_starttime",starttime);
            result.put("unserve_endtime",endtime);


            log.debug("【防伪维护时间查询API】成功返回的JSON报文："+result);
            //写入接口日志表
            String logsql="insert into dg_api_log(rec_guid,api_type,request_body,back_body,request_time,remarks)values(uuid(),?,?,?,now(),?)";
            jt.update(logsql, "apiSecurityTime",inData.toString(),result.toString(),"防伪维护时间查询API接口");



        } catch (Exception e) {
            log.error("【防伪维护时间查询API】数据处理错误:", e);
            result.put("status", -1);
            result.put("msg", "数据处理错误[" + e.getMessage() + "]");
            result.put("data", "{}");

            //写入接口日志表
            String logsql="insert into dg_api_log(rec_guid,api_type,request_body,back_body,request_time,remarks)values(uuid(),?,?,?,now(),?)";
            jt.update(logsql, "apiSecurityTime",inData.toString(),result.toString(),"防伪维护时间查询API接口");
        }









        return result;
    }
}
